﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Microsoft.Windows.Controls;
//using ProductList.ScrumMagicDataSetTableAdapters;


namespace ScrumMagic
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ProductCardForm : ScrumMagic.CommonForm
    {
        int productId;

        public ProductCardForm(int productId)
        {
            this.productId = productId;
            InitializeComponent();
        }

        static string StrDate;
        static string StrStatus;
        static string StrMaster;
        static string StrOwner;

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            try
            {
                SqlCommand productCommand = DBController.MainConnection.CreateCommand();
                productCommand.CommandText =
                    @"Select Product.Name, Product.Start_Date, [Status].[Name] as 'Status.Name', 
                    u1.FirstName as 'ProductOwnerUser.FirstName', u1.LastName as 'ProductOwnerUser.LastName', 
                    u2.FirstName as 'ScrumMasterUser.FirstName', u2.LastName as 'ScrumMasterUser.LastName' 
                    from Product 
                    inner join [Status] on [Status].Id = Product.Status_Id 
                    left join RoleInProduct rp1 on rp1.Product_Id = Product.id 
                        inner join [Role] r1 on rp1.Role_Id = r1.Id and r1.[Name]='ProductOwner' 
                        inner join [User] u1 on rp1.User_Id = u1.Id
                    left join RoleInProduct rp2 on rp2.Product_Id = Product.id 
                        inner join [Role] r2 on rp2.Role_Id = r2.Id and r2.[Name]='ScrumMaster' 
                        inner join [User] u2 on rp2.User_Id = u2.Id 
                    where Product.id = @productId";
                productCommand.Parameters.AddWithValue("@productId", productId);
                DataRow productRow = DBController.ExecuteDataRow(productCommand);
                label_Name.Content = (string)productRow["Name"];
                textBox1.Text = ((DateTime)productRow["Start_Date"]).ToShortDateString();
                textBox2.Text = (string)productRow["Status.Name"];
                textBox3.Text = (string)productRow["ProductOwnerUser.FirstName"]
                    + (string)productRow["ProductOwnerUser.LastName"];
                textBox4.Text = (string)productRow["ScrumMasterUser.FirstName"]
                    + (string)productRow["ScrumMasterUser.LastName"];

                SqlCommand usersCommand = DBController.MainConnection.CreateCommand();

                usersCommand.CommandText =
                    @"Select FirstName,LastName,EMail,[Name] from [User],[Role],RoleInProduct where 
                    [Role].Id=RoleInProduct.Role_Id and
                    [User].Id=RoleInProduct.User_id and 
                    RoleInProduct.Product_Id= @productId";
                usersCommand.Parameters.AddWithValue("@productId", productId);

                DataTable usersTable = DBController.ExecuteDataTable(usersCommand);
                dataGrid1.ItemsSource = usersTable.DefaultView;
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.ToString());
                //Close();
            }

                 
        }

   
        private void OnGridMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

    }
}